#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

void swap(int &a, int &b)
{
    int tmp = a;
    a = b;
    b = tmp;
}


int firstMissing(int num[], int size)
{
    for (int i = 0; i < size; i++) {
	if (num[i] < 0 || num[i] >= size) continue;
        while((num[i] >= 0 && num[i] < size) && num[i] != i && num[num[i]] != num[i]) {
	    swap(num[num[i]], num[i]);
        }
    }
    int rtn = -1;
    for (int i = 1; i < size; i++) 
        if (num[i] != i) return i; 
    return size; 
}

int main(int argc, char **argv)
{
    //int a[] = {-1, -2,-3,-4,0,1,2,3,6,4,6,8,9};
    int a[] = {0,1,2,3,6,4,8,8,5,7};
    cout << firstMissing(a, sizeof(a)/sizeof(int)) << endl;
}
